import java.util.ArrayList;
import java.util.List;

public class PostorderTraversal {
    public int[] postorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        postorderTraversalChild(root, list);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }

    private void postorderTraversalChild(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        postorderTraversalChild(root.left, list);
        postorderTraversalChild(root.right, list);
        list.add(root.val);
    }
}
